package org.friping.mytool.helper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import org.androidannotations.annotations.EBean;
import org.friping.mytool.views.picker.PickerCurrencyActivity_;
import org.friping.mytool.views.picker.PickerDateActivity_;
import org.friping.mytool.views.picker.PickerDayOfMonthActivity_;
import org.friping.mytool.views.picker.PickerNoteActivity_;
import org.friping.mytool.views.picker.PickerSwitchActivity_;
import org.friping.mytool.views.picker.PickerTextActivity_;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EBean
public class UITypeHelper {

    public enum Type{
        String,
        Boolean,
        Integer,
        DayOfMonth,
        CircleMonth,
        Currency,
        Date,
        Text;
    }

    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    public String getDisplayValue(Type type, Object value){
        String val = "";

        if(value == null){
            return val;
        }
        switch (type){
            case String:
            case Text:
                val = (String) value;
                break;
            case Boolean:
                val = Boolean.TRUE.equals(value) ? "是" : "否";
                break;
            case Integer:
            case DayOfMonth:
            case CircleMonth:
                val = value.toString();
                break;
            case Currency:
                NumberFormat nf = NumberFormat.getCurrencyInstance();
                val = nf.format(value);
                break;
            case Date:
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                if(value instanceof String){
                    val = (String) value;
                }else{
                    val = df.format(value);
                }
                break;
        }
        return val;
    }

    public String dayOfMonthText(Integer value, boolean isLunar){
        String val = "";
        if(isLunar){
            val = LunarCalendar.getChinaDayString(value);
        }else{
            val = value + "日";
        }
        return val;
    }

    public Object fromString(Type type, String value){
        Object val = null;
        if(value != null){
            switch (type){
                case String:
                case Text:
                    val = value;
                    break;
                case Boolean:
                    val = Boolean.valueOf(value);
                    break;
                case Integer:
                case DayOfMonth:
                case CircleMonth:
                    val = Integer.parseInt(value);
                    break;
                case Currency:
                    val = Float.parseFloat(value);
                    break;
                case Date:
                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        val = df.parse(value);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
        return val;
    }

    public void showByType(Activity context, String type, Object value, Bundle info){
        Intent intent = new Intent();
        intent.putExtras(info);
        showByType(context, Type.valueOf(type), value, intent, null);
    }


    public void showByType(Activity context, String type, Object value, Bundle info, Integer requestCode){
        Intent intent = new Intent();
        intent.putExtras(info);
        showByType(context, Type.valueOf(type), value, intent, requestCode);
    }


    public void showByType(Activity context, Type type, Object value, Intent intent, Integer requestCode){
        requestCode = requestCode == null ? Constants.PICKER_REQUEST_CODE : requestCode;
        switch (type){
            case String:
                intent.putExtra("value", (String) value);
                intent.setClass(context, PickerTextActivity_.class);
                break;
            case Text:
                intent.putExtra("value", (String) value);
                intent.setClass(context, PickerNoteActivity_.class);
                break;
            case Boolean:
                intent.putExtra("value", Boolean.valueOf(String.valueOf(value)));
                intent.setClass(context, PickerSwitchActivity_.class);
                break;
            case Integer:
                Integer v = value == null ? null : Integer.valueOf(String.valueOf(value));
                intent.putExtra("value", v);
                intent.setClass(context, PickerTextActivity_.class);
                break;
            case DayOfMonth:
                v = value == null ? null : Integer.valueOf(String.valueOf(value));
                intent.putExtra("value", v);
                intent.setClass(context, PickerDayOfMonthActivity_.class);
                break;
            case CircleMonth:
                Float fv = value == null ? null : Float.valueOf(String.valueOf(value));
                intent.putExtra("value", fv);
                intent.setClass(context, PickerTextActivity_.class);
                break;
            case Currency:
                fv = value == null ? null : Float.valueOf(String.valueOf(value));
                intent.putExtra("value", fv);
                intent.setClass(context, PickerCurrencyActivity_.class);
                break;
            case Date:
                Date date = null;
                try {
                    date = df.parse(String.valueOf(value));
                    intent.putExtra("value", date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                intent.setClass(context, PickerDateActivity_.class);
                break;
        }
        context.startActivityForResult(intent, requestCode);
    }


    public Map getValuesForEntity(List values){
        Map m = new HashMap();
        for(Map o : (List<Map>)values){
            m.put(o.get("name"), o.get("value"));
        }
        return m;
    }
}
